package com.gcxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gcxy.dto.PenDTO;
import com.gcxy.entity.Pen;
import com.gcxy.entity.Ranch;
import com.gcxy.mapper.PenMapper;
import com.gcxy.mapper.RanchMapper;
import com.gcxy.service.PenService;
import com.gcxy.service.RanchService;
import com.sun.org.apache.xml.internal.utils.RawCharacterHandler;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiang
 * @since 2023-12-12
 */
@Service
public class PenServiceImpl extends ServiceImpl<PenMapper, Pen> implements PenService {


    @Autowired
    private PenMapper penMapper;

    @Autowired
    private RanchMapper ranchMapper;


    /**
     * 分页模糊查询圈舍
     *
     * @param penDTO
     * @return
     */
    @Override
    public Page<PenDTO> getPenByPage(PenDTO penDTO) {
        QueryWrapper<PenDTO> lqw = new QueryWrapper<PenDTO>();
        lqw.like(penDTO.getRanchName() != null, "r.ranch_name", penDTO.getRanchName())
                .like(penDTO.getPenName() != null,"p.pen_name", penDTO.getPenName())
                .eq(penDTO.getIsEnable() != null, "p.is_enable", penDTO.getIsEnable());

        Page<PenDTO> page = new Page<>(penDTO.getPageNumber(), penDTO.getPageSize());

        Page<PenDTO> penDTOPage = penMapper.selectAllPen(page, lqw);
        return penDTOPage;
    }



    /**
     * 添加圈舍
     *
     * @param pen
     * @return
     */
    @Override
    public boolean addPen(Pen pen) {
        LambdaQueryWrapper<Pen> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Pen::getRanchId, pen.getRanchId())
                .eq(Pen::getPenName, pen.getPenName());

        Pen result = penMapper.selectOne(lqw);
        if (!ObjectUtils.isEmpty(result)) {
            return false;
        }
        penMapper.insert(pen);
        return true;
    }


    /**
     * 修改圈舍
     *
     * @param pen
     * @return
     */

    @Override
    public boolean updatePen(Pen pen) {
        LambdaQueryWrapper<Pen> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Pen::getRanchId, pen.getRanchId())
                .eq(Pen::getPenName, pen.getPenName());

        Pen result = penMapper.selectOne(lqw);
        if (!ObjectUtils.isEmpty(result)) {
            return false;
        }
        penMapper.update(pen, null);
        return true;
    }


    /**
     * 启用禁用圈舍
     * @param pen
     * @return
     */
    @Override
    public boolean updateIsEnable(Pen pen) {
        Ranch ranch = ranchMapper.selectById(pen.getRanchId());
        Pen pen1 = penMapper.selectById(pen.getPenId());
        if(ranch.getIsEnable() == 1){
            if(pen1.getIsEnable() == 0){
                pen1.setIsEnable(1);
            }else {
                pen1.setIsEnable(0);
            }
            penMapper.updateById(pen1);
        }
        return false;
    }


    /**
     * 获取圈舍名称列表
     * @return
     */
    @Override
    public List<String> getPenNameList() {
        LambdaQueryWrapper<Pen> lqw = new LambdaQueryWrapper<>();
        lqw.select(Pen::getPenName);

        return penMapper.selectList(lqw).stream().map(Pen::getPenName).collect(Collectors.toList());
    }
}
